<?php
namespace app\models\events;

use yii\db\ActiveRecord;
use yii\base\Behavior;
use yii\web\NotFoundHttpException;

class UserEvent extends Behavior
{
    public function events()
    {
        return [
            ActiveRecord::EVENT_BEFORE_INSERT => 'beforeInsert',
            ActiveRecord::EVENT_BEFORE_DELETE=>'beforeDelete',
        ];
    }

    /**
     * 添加之前
     * @param $event
     * @return bool
     */
    public function beforeInsert($event)
    {
        /* @var $user \app\models\User*/
        $user = $this->owner;
        $user->auth_key = \Yii::$app->security->generateRandomString();
        return true;
    }

    public function beforeDelete($event)
    {
        /* @var $user \app\models\User*/
        $user = $this->owner;
        \Yii::$app->authManager->revokeAll($user->id);
        return true;
    }
}